package com.azunitech.intercpt.ejb;

import javax.annotation.Resource;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;

public class JMSInterceptor {
	private static Logger logger = Logger.getLogger(ACLInterceptor.class);
	
	@Resource(lookup = "CF-INTERCEPTOR-001")
	private QueueConnectionFactory connectionFactory;
	
	@Resource(lookup = "Queue-Interceptor-001")
	private Queue queue;
	
	@AroundInvoke
	public Object context(InvocationContext ic) throws Exception {
		 try {
			 logger.info("This is called");
				try {
					QueueConnection connection = connectionFactory.createQueueConnection();
					QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
					QueueSender qsender = session.createSender(queue);
					TextMessage msg = session.createTextMessage();
					msg.setText("This is from the JMSInterceptor");
					qsender.send(msg);
					connection.close();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			 
			 
			 return ic.proceed();
		 }
		 finally{
			 
		 }
	}
}
